<?php
/*
Plugin Name: Carbon Paper :: Categories Table
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: Categories Table with posts count. 
Version: 1.0
Author: Bertrand SCARPA
Author URI: http://URI_Of_The_Plugin_Author
*/

register_activation_hook( __FILE__, array('Categories_table', 'activate'));
register_deactivation_hook( __FILE__, array('Categories_table', 'deactivate'));

require_once( ABSPATH . WPINC . '/class.wp-multiwidget.php');

class Categories_table extends MultiWidget {

  function Categories_table()
  {
    $this->MultiWidget(
        'cattable', // id_base
        'Carbon Paper :: Categories Table', // name
        array('description'=>__('Categories Table'))
      );
  }

 // Echo the actual widget content. 
 function widget($args, $instance){
    
	$cat_args = array('orderby' => 'name', 'show_count' => 1, 'title_li' => '', 'echo' => 0, 'depth' => -1);
	$cat = wp_list_categories($cat_args);
	$cat = preg_replace("#<a([^>]*)>#", "<a$1><span>", $cat);
	$cat = preg_replace("#</a>\s\((\d+)\)#", "</span>$1</a>", $cat);
	$table = preg_split('#</li>#', $cat);
	$nbcol = 4;
        $nbrow =  ((int) count($table) / $nbcol);

	?>
	<div class="clear"></div>
	<div id="cat-table">
		<h3><?php echo $instance['cattable_title']; ?></h3>
		<table cellspacing="0" cellpadding="0" border="0"><tbody><tr>
			<?php
			for( $i = 0; $i < $nbcol ; $i++){
				echo '<td><ul>';
				for( $j = $nbrow * $i ; $j < $nbrow * ($i + 1); $j++)
					echo $table[$j] . '</li>';
				echo '</ul></td><td class="shim"></td>';
			}
			?>
		</tr></tbody></table>
    	</div><!-- /.cat-table -->
	<?php
  }

  // Update a particular instance.
  // This function should check that $new_instance is set correctly.
  // The newly calculated value of $instance should be returned.
  function control_update($new_instance, $old_instance)
  {
    if( !isset($new_instance['cattable_title']) ) // user clicked cancel
        return false;
    $instance = $old_instance;
    $instance['cattable_title'] = wp_specialchars( $new_instance['cattable_title'] );
    return $instance;
  }


  // Echo a control form for the current instance.
  // The form has inputs with names like widget-ID_BASE[$number][FIELD_NAME]
  // so that all data for that instance of the widget are stored in one
  // $_POST variable: $_POST['widget-ID_BASE'][$number]
  function control_form($instance){
   ?>
    <p>
     <label for="<?php echo $this->get_field_id('cattable_title') ?>">
	Titre Table :
      <input class="widefat" id="<?php echo $this->get_field_id('cattable_title') ?>"
       name="<?php echo $this->get_field_name('cattable_title') ?>" type="text"
       value="<?php echo htmlspecialchars($instance['cattable_title'],ENT_QUOTES) ?>" />
     </label>

     <input type="hidden" id="<?php echo $this->get_field_id('submit') ?>"
      name="<?php echo $this->get_field_name('submit') ?>" value="1" />

    </p>
<?php
  }

} // End Class Categories_table

// Finally create an object for the widget-type and register it.
$cattable = new Categories_table();
add_action("widgets_init", array($cattable, 'register'));
?>